home *** CD-ROM | disk | FTP | other *** search
/ Super PC 33 / Super PC 33 (Shareware).iso / spc / sonido / timidity / source / raw_audi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-06  |  3.2 KB  |  151 lines

  1. /*
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <titoivon@snakemail.hut.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     raw_audio.c
  21.  
  22.     Functions to output raw sound data to a file or stdout.
  23.  
  24. */
  25.  
  26. #ifdef __WIN32__
  27. #include <stdlib.h>
  28. #include <io.h>
  29. #include <string.h>
  30. #else
  31. #include <unistd.h>
  32. #endif
  33. #include <fcntl.h>
  34. #include <errno.h>
  35.  
  36. #ifdef __FreeBSD__
  37. #include <stdio.h>
  38. #endif
  39.  
  40. #include "config.h"
  41. #include "output.h"
  42. #include "controls.h"
  43.  
  44. #ifdef __WIN32__
  45. #define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
  46. #else
  47. #define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC
  48. #endif
  49.  
  50. static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
  51. static void close_output(void);
  52. static void output_data(int32 *buf, int32 count);
  53. static void flush_output(void);
  54. static void purge_output(void);
  55.  
  56. /* export the playback mode */
  57.  
  58.  
  59. #define dpm raw_play_mode
  60.  
  61. PlayMode dpm = {
  62.   DEFAULT_RATE, PE_16BIT|PE_SIGNED,
  63.   -1,
  64.   {0},
  65.   "raw waveform data", 'r',
  66.   "output.raw",
  67.   open_output,
  68.   close_output,
  69.   output_data,
  70.   flush_output,
  71.   purge_output  
  72. };
  73.  
  74. /*************************************************************************/
  75.  
  76. static int open_output(void)
  77. {
  78.   if (dpm.encoding & PE_ULAW)
  79.     dpm.encoding &= ~PE_16BIT;
  80.  
  81.   if (!(dpm.encoding & PE_16BIT))
  82.      dpm.encoding &= ~PE_BYTESWAP;
  83.  
  84.   if (dpm.name && dpm.name[0]=='-' && dpm.name[1]=='\0')
  85.      dpm.fd=1; /* data to stdout */
  86.   else
  87.      {
  88.         /* Open the audio file */
  89.         dpm.fd=open(dpm.name, OPEN_MODE, 0644);
  90.         if (dpm.fd<0)
  91.     {
  92.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
  93.              dpm.name, sys_errlist[errno]);
  94.       return -1;
  95.     }
  96.      }
  97.   return 0;
  98. }
  99.  
  100. static void output_data(int32 *buf, int32 count)
  101. {
  102.   if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
  103.  
  104.   if (dpm.encoding & PE_16BIT)
  105.      {
  106.         if (dpm.encoding & PE_BYTESWAP)
  107.     {
  108.       if (dpm.encoding & PE_SIGNED)
  109.          s32tos16x(buf, NULL, count);
  110.       else
  111.          s32tou16x(buf, NULL, count);
  112.     }
  113.         else
  114.     {
  115.       if (dpm.encoding & PE_SIGNED)
  116.          s32tos16(buf, NULL, count);
  117.       else
  118.          s32tou16(buf, NULL, count);
  119.     }
  120.  
  121.         while ((-1==write(dpm.fd, buf, count * 2)) && errno==EINTR)
  122.     ;
  123.      }
  124.   else
  125.      {
  126.         if (dpm.encoding & PE_ULAW)
  127.     {
  128.       s32toulaw(buf, NULL, count);
  129.     }
  130.         else
  131.     {
  132.       if (dpm.encoding & PE_SIGNED)
  133.          s32tos8(buf, NULL, count);
  134.       else
  135.          s32tou8(buf, NULL, count);
  136.     }
  137.       
  138.       while ((-1==write(dpm.fd, buf, count)) && errno==EINTR)
  139.     ;
  140.     }
  141. }
  142.  
  143. static void close_output(void)
  144. {
  145.   if (dpm.fd != 1) /* We don't close stdout */
  146.     close(dpm.fd);
  147. }
  148.  
  149. static void flush_output(void) { }
  150. static void purge_output(void) { }
  151.